{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 语音识别\n",
    "\n",
    "百度百科:语音识别技术就是让机器通过识别和理解过程把语音信号转变为相应的文本或命令的高技术。\n",
    "\n",
    "百度AI语音识别产品介绍链接:https://ai.baidu.com/tech/speech\n",
    "\n",
    "百度AI语音识别python接口使用说明:https://ai.baidu.com/ai-doc/SPEECH/1k4o0bmc7"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 循环神经网络——RNN\n",
    "\n",
    "语音识别一般采用：循环神经网络——RNN。\n",
    "\n",
    "关于RNN，可以自行百度。下面我给出几个参考连接：\n",
    "\n",
    "(1)百度百科:https://baike.baidu.com/item/%E5%BE%AA%E7%8E%AF%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/23199490?fromtitle=RNN&fromid=5707183&fr=aladdin\n",
    "\n",
    "(2)从BP神经网络到循环神经网络RNN再到LSTM:https://zhuanlan.zhihu.com/p/86358259\n",
    "\n",
    "(3)一文搞懂RNN（循环神经网络）基础篇:https://zhuanlan.zhihu.com/p/30844905\n",
    "\n",
    "(4)RNN 结构详解:https://www.jiqizhixin.com/articles/2018-12-14-4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 开始我们的实验\n",
    "## (1)登录百度AI开发平台，选择“语音技术”，使用“创建应用”建立一个语音技术应用。\n",
    "\n",
    "获取应用的AppID、API Key、Secret Key，注意:Secret Key要先选择显示，然后复制其现实的字符。\n",
    "\n",
    "例如我创建的语音识别应用获取的AppID、API Key、Secret Key如下：\n",
    "\n",
    "    \"\"\" 公开课有限量的语音识别和语音合成，必须更换为自己的api接口　\"\"\"\n",
    "    SP_APP_ID = '20617745 '\n",
    "    SP_API_KEY = 'oDwWnVAyLDcb2wVnGR9tSmmh'\n",
    "    SP_SECRET_KEY = '6T5kl7lQC3FYcDrXQbu22xr27EjFYsnf'\n",
    "\n",
    "注：对于“语音识别”功能来说，本步必须的，因为百度AI开发平台对于“语音识别”中普通话识别，只提供50000次调用试用。您使用的课程时，可能别人已经用完了这50000次。\n",
    "\n",
    "百度AI开发平台注册链接:https://login.bce.baidu.com/?account=&redirect=http%3A%2F%2Fconsole.bce.baidu.com%2F%3Ffromai%3D1#/aip/overview\n",
    "\n",
    "## (2)熟悉我封装的百度AI语音库的模块\n",
    "\n",
    "在代码中输入如下代码，导入我的封装的模块\n",
    "\n",
    "    import sys\n",
    "    sys.path.append('../baidu_api_lib')\n",
    "    from baidu_speech_recognition import baidu_speech_2_word\n",
    "\n",
    "封装库提供两个方法：初始化方法，文字到语音的语音识别方法。\n",
    "\n",
    "### <1>初始化方法，需要传入三个参数，就是上步我们创建“语音技术”应用获取的三个参数\n",
    "\n",
    "参数信息如下：\n",
    "    \n",
    "    \"\"\" 公开课有限量的语音识别和语音合成，必须更换为自己的api接口　\"\"\"\n",
    "    SP_APP_ID = '20617745 '\n",
    "    SP_API_KEY = 'oDwWnVAyLDcb2wVnGR9tSmmh'\n",
    "    SP_SECRET_KEY = '6T5kl7lQC3FYcDrXQbu22xr27EjFYsnf'\n",
    "\n",
    "调用方式：\n",
    "    \n",
    "    #传入百度AI的参数给语音识别模块\n",
    "    sp_2_wd = baidu_speech_2_word(APP_ID, API_KEY, SECRET_KEY)\n",
    "    \n",
    "    初始化完毕后，获取到sp_2_wd对象，操作该对象就能进行语音识别。\n",
    "    \n",
    "### <2>语音到文字的语音识别方法\n",
    "\n",
    "这是我封装百度语音识别方法的函数原型：\n",
    "    \n",
    "    speech_2_word(sound_file, rate = 16000, dev_pid = 1537):                 \n",
    "    speech_2_word包含3个参数，其中sound_file为必须填写的内容。\n",
    "    sound_file:要语音识别的音频文件，需要用单引号包裹文字，例如:'repeater.mp3'，就是要识别'repeater.mp3'这个音频文件。\n",
    "    rate:采样率，16000、8000，固定值。\n",
    "    dev_pid:\n",
    "        1537——普通话(纯中文识别)\n",
    "        1737——英语\n",
    "        1637——粤语\n",
    "        1837——四川话\n",
    "        1936——普通话远场\n",
    "\n",
    "## (3)熟悉麦克风录音的模块    \n",
    "    \n",
    "我从网上找个一个不错的模块放到我的python模块库中。\n",
    "    \n",
    "在代码中输入如下代码，导入我的封装的模块\n",
    "\n",
    "    import sys\n",
    "    sys.path.append('../baidu_api_lib')\n",
    "    import voice_record\n",
    "    \n",
    "封装库只提供一种方法录音方法，这是模块函数原型。\n",
    "\n",
    "    recording(filename, time=0, threshold=7000):\n",
    "    包含3个参数，其中filename为必须填写的内容。\n",
    "    filename:录音后保存音频文件名。\n",
    "    time:录音时间,如果指定时间，按时间来录音，默认为自动识别是否结束录音\n",
    "    threshold: 判断录音结束的阈值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "######################################################\n",
    "#\n",
    "# 先感受下学说话的小派,选择本cell，按shirt+enter键运行本模块\n",
    "#\n",
    "######################################################\n",
    "#!/usr/bin/env python\n",
    "# coding: utf-8\n",
    "\n",
    "# In[ ]:\n",
    "\n",
    "\n",
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "#\n",
    "# Copyright (c) 2020 Taste all Pi.\n",
    "#\n",
    "# Licensed under the GNU General Public License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#   http://www.gnu.org/licenses/gpl-2.0.html\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "#导入标准库\n",
    "import sys\n",
    "import os\n",
    "import time\n",
    "\n",
    "#导入自定义库\n",
    "sys.path.append('../baidu_api_lib')\n",
    "from baidu_speech_recognition import baidu_speech_2_word\n",
    "from baidu_sound import baidu_word_2_sound\n",
    "import voice_record\n",
    "\n",
    "\"\"\" 公开课有限量的语音识别和语音合成，必须更换为自己的api接口　\"\"\"\n",
    "SP_APP_ID = '20617745 '\n",
    "SP_API_KEY = 'oDwWnVAyLDcb2wVnGR9tSmmh'\n",
    "SP_SECRET_KEY = '6T5kl7lQC3FYcDrXQbu22xr27EjFYsnf'\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    try:\n",
    "        #传入百度AI的参数给语音识别模块\n",
    "        sp_2_wd = baidu_speech_2_word(SP_APP_ID, SP_API_KEY, SP_SECRET_KEY)\n",
    "        \n",
    "        #传入百度AI的参数给语音合成模块\n",
    "        word_2_sound = baidu_word_2_sound(SP_APP_ID, SP_API_KEY, SP_SECRET_KEY)\n",
    "        \n",
    "        while True:\n",
    "            #进行麦克风录音，每次录音6秒\n",
    "            voice_record.recording(\"repeater_raw.wav\", 6)\n",
    "\n",
    "            #将音频采样率更改为百度需要的16000\n",
    "            os.system('sox repeater_raw.wav -r 16000 repeater.wav')\n",
    "            \n",
    "            #对音频文件进行分析\n",
    "            msg = sp_2_wd.speech_2_word('repeater.wav')\n",
    "\n",
    "            #语音识别正确，则进行语音合成和输出\n",
    "            if msg[\"err_no\"] == 0:\n",
    "                #输出语音识别后，转化的文字\n",
    "                print(msg[\"result\"])\n",
    "\n",
    "                #进行语音合成\n",
    "                word_2_sound.trans_word_to_sound(msg[\"result\"],'repeater.wav')\n",
    "\n",
    "                #播放合成的语音\n",
    "                os.system('mplayer ' + 'repeater.wav')\n",
    "            \n",
    "            #延时\n",
    "            time.sleep(1)\n",
    "            \n",
    "        #删除临时文件    \n",
    "        os.remove('repeater_raw.wav')\n",
    "        os.remove('repeater.wav')\n",
    "        \n",
    "    except KeyboardInterrupt:\n",
    "        os.remove('repeater_raw.wav')\n",
    "        os.remove('repeater.wav')\n",
    "        print(\"任务被终止了\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 让我们临摹代码，学习“语音识别”功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# #导入标准库\n",
    "# import sys\n",
    "\n",
    "# import os\n",
    "\n",
    "# import time\n",
    "\n",
    "\n",
    "# #导入自定义库\n",
    "# sys.path.append('../baidu_api_lib')\n",
    "\n",
    "# from baidu_speech_recognition import baidu_speech_2_word\n",
    "\n",
    "# from baidu_sound import baidu_word_2_sound\n",
    "\n",
    "# import voice_record\n",
    "\n",
    "\n",
    "# \"\"\" 公开课有限量的语音识别和语音合成，必须更换为自己的api接口　\"\"\"\n",
    "# SP_APP_ID = '20617745 '\n",
    "\n",
    "# SP_API_KEY = 'oDwWnVAyLDcb2wVnGR9tSmmh'\n",
    "\n",
    "# SP_SECRET_KEY = '6T5kl7lQC3FYcDrXQbu22xr27EjFYsnf'\n",
    "\n",
    "\n",
    "\n",
    "# if __name__ == \"__main__\":\n",
    "\n",
    "#     try:\n",
    "\n",
    "#         #传入百度AI的参数给语音识别模块\n",
    "#         sp_2_wd = baidu_speech_2_word(SP_APP_ID, SP_API_KEY, SP_SECRET_KEY)\n",
    "\n",
    "        \n",
    "#         #传入百度AI的参数给语音合成模块\n",
    "#         word_2_sound = baidu_word_2_sound(SP_APP_ID, SP_API_KEY, SP_SECRET_KEY)\n",
    "\n",
    "        \n",
    "#         while True:\n",
    "\n",
    "#             #进行麦克风录音，每次录音6秒\n",
    "#             voice_record.recording(\"repeater_raw.wav\", 6)\n",
    "\n",
    "\n",
    "#             #将音频采样率更改为百度需要的16000\n",
    "#             os.system('sox repeater_raw.wav -r 16000 repeater.wav')\n",
    "\n",
    "            \n",
    "#             #对音频文件进行分析\n",
    "#             msg = sp_2_wd.speech_2_word('repeater.wav')\n",
    "\n",
    "\n",
    "#             #语音识别正确，则进行语音合成和输出\n",
    "#             if msg[\"err_no\"] == 0:\n",
    "\n",
    "#                 #输出语音识别后，转化的文字\n",
    "#                 print(msg[\"result\"])\n",
    "\n",
    "\n",
    "#                 #进行语音合成\n",
    "#                 word_2_sound.trans_word_to_sound(msg[\"result\"],'repeater.wav')\n",
    "\n",
    "\n",
    "#                 #播放合成的语音\n",
    "#                 os.system('mplayer ' + 'repeater.wav')\n",
    "\n",
    "            \n",
    "#             #延时\n",
    "#             time.sleep(1)\n",
    "\n",
    "            \n",
    "#         #删除临时文件    \n",
    "#         os.remove('repeater_raw.wav')\n",
    "\n",
    "#         os.remove('repeater.wav')\n",
    "\n",
    "        \n",
    "#     except KeyboardInterrupt:\n",
    "\n",
    "#         os.remove('repeater_raw.wav')\n",
    "\n",
    "#         os.remove('repeater.wav')\n",
    "\n",
    "#         print(\"任务被终止了\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 课后练习\n",
    "## 1.当前我们使用识别的是普通话，尝试识别英文、四川话、粤语试试\n",
    "    \n",
    "    speech_2_word(sound_file, rate = 16000, dev_pid = 1537): \n",
    "    dev_pid:\n",
    "        1537——普通话(纯中文识别)\n",
    "        1737——英语\n",
    "        1637——粤语\n",
    "        1837——四川话\n",
    "        1936——普通话远场\n",
    "## 2.当前我们使用默认录音6秒，尝试使用自动识别，调节阈值找到自己麦克风最合适的阈值\n",
    "\n",
    "    recording(filename, time=0, threshold=7000):\n",
    "    threshold: 判断录音结束的阈值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "######################################################\n",
    "#\n",
    "# 更改参数，体验不同的“小派复读机”\n",
    "#\n",
    "######################################################\n",
    "\n",
    "#!/usr/bin/env python\n",
    "# coding: utf-8\n",
    "\n",
    "# In[ ]:\n",
    "\n",
    "\n",
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "#\n",
    "# Copyright (c) 2020 Taste all Pi.\n",
    "#\n",
    "# Licensed under the GNU General Public License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#   http://www.gnu.org/licenses/gpl-2.0.html\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "#导入标准库\n",
    "import sys\n",
    "import os\n",
    "import time\n",
    "\n",
    "#导入自定义库\n",
    "sys.path.append('../baidu_api_lib')\n",
    "from baidu_speech_recognition import baidu_speech_2_word\n",
    "from baidu_sound import baidu_word_2_sound\n",
    "import voice_record\n",
    "\n",
    "\"\"\" 公开课有限量的语音识别和语音合成，必须更换为自己的api接口　\"\"\"\n",
    "SP_APP_ID = '20617745 '\n",
    "SP_API_KEY = 'oDwWnVAyLDcb2wVnGR9tSmmh'\n",
    "SP_SECRET_KEY = '6T5kl7lQC3FYcDrXQbu22xr27EjFYsnf'\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    try:\n",
    "        #传入百度AI的参数给语音识别模块\n",
    "        sp_2_wd = baidu_speech_2_word(SP_APP_ID, SP_API_KEY, SP_SECRET_KEY)\n",
    "        \n",
    "        #传入百度AI的参数给语音合成模块\n",
    "        word_2_sound = baidu_word_2_sound(SP_APP_ID, SP_API_KEY, SP_SECRET_KEY)\n",
    "        \n",
    "        while True:\n",
    "            #进行麦克风录音，每次录音6秒\n",
    "            voice_record.recording(\"repeater_raw.wav\", 6)\n",
    "            \n",
    "            #进行麦克风录音自动识别功能，当前配置麦克风无声音阈值为8000\n",
    "            #voice_record.recording(\"repeater.mp3\", 0, threshold=12000)\n",
    "\n",
    "            #将音频采样率更改为百度需要的16000\n",
    "            os.system('sox repeater_raw.wav -r 16000 repeater.wav')\n",
    "            \n",
    "            #对音频文件进行分析\n",
    "            msg = sp_2_wd.speech_2_word('repeater.wav')\n",
    "\n",
    "            #语音识别正确，则进行语音合成和输出\n",
    "            if msg[\"err_no\"] == 0:\n",
    "                #输出语音识别后，转化的文字\n",
    "                print(msg[\"result\"])\n",
    "\n",
    "                #进行语音合成\n",
    "                word_2_sound.trans_word_to_sound(msg[\"result\"],'repeater.wav')\n",
    "\n",
    "                #播放合成的语音\n",
    "                os.system('mplayer ' + 'repeater.wav')\n",
    "            \n",
    "            #延时\n",
    "            time.sleep(1)\n",
    "            \n",
    "        #删除临时文件    \n",
    "        os.remove('repeater_raw.wav')\n",
    "        os.remove('repeater.wav')\n",
    "        \n",
    "    except KeyboardInterrupt:\n",
    "        os.remove('repeater_raw.wav')\n",
    "        os.remove('repeater.wav')\n",
    "        print(\"任务被终止了\")    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
